package cn.hospital.seviceimpl;

import cn.hospital.dao.TemplateMapper;
import cn.hospital.entity.MeaningUser;
import cn.hospital.entity.Template;
import cn.hospital.service.TemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TemplateServiceImpl implements TemplateService {
    @Resource
    private TemplateMapper templateMapper;

    /**
     * 添加模板
     * @param title
     * @param field_name
     * @param type_of_frame
     * @param contents_of_the_box
     * @param session
     * @return
     */
    public Integer insertSelective(String[] title, String[] field_name, String[] type_of_frame,
                                   String[] contents_of_the_box, String createName, HttpSession session) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
        Date date = new Date();
        String page = sdf.format(date);
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        String username = meaningUser.getUsername();
        page = page + "-" + username + "," + createName;
        Template template = new Template();
        Integer count = 0;
        for (int i = 0; i < title.length; i++) {
            template.setTitle(title[i]);
            template.setFieldName(field_name[i]);
            template.setTypeOfFrame(type_of_frame[i]);
            template.setContentsOfTheBox(contents_of_the_box[i]);
            template.setPageId(page);
            template.setUpdatedDate(date);
            template.setCreationDate(date);
            template.setUpdate(username);
            template.setDepartmentId(meaningUser.getDepartmentId());
            template.setVersionNumber("1.0");
            template.setState(1);
            count = templateMapper.insertSelective(template);
        }
        return count;
    }

    /**
     * 查询所有模板数据
     * @param map
     * @return
     */
    public List<Template> selectTemplate(Map<String, Object> map) {
        return templateMapper.selectTemplate(map);
    }

    /**
     * 查询模板数量
     * @param map
     * @return
     */
    public Long selectTemplateCount(Map<String, Object> map) {
        return templateMapper.selectTemplateCount(map);
    }

    /**
     * 修改状态码
     * @param pageId
     * @return
     */
    @Override
    public Integer updatState(String pageId, String versionNumber,String updatedDate) {
        Integer count = templateMapper.updatState(pageId, versionNumber);
        count = selectByPageId(pageId,updatedDate);
        return count;
    }

    /**
     * 删除模板
     * @param pageId
     * @param versionNumber
     * @return
     */
    @Override
    public Integer deleteTemplate(String pageId, String versionNumber) {
        return templateMapper.deleteTemplate(pageId, versionNumber);
    }

    /**
     * 查询单个模板数据
     * @param pageId
     * @param versionNumber
     * @return
     */
    @Override
    public List<Template> selectTemplatem(String pageId, String versionNumber) {
        return templateMapper.selectTemplatem(pageId, versionNumber);
    }

    /**
     * 修改及添加
     * @param title
     * @param field_name
     * @param type_of_frame
     * @param contents_of_the_box
     * @param createName
     * @param session
     * @return
     */
    @Override
    public Integer insertSelectiveTemplatem(String[] title, String[] field_name, String[] type_of_frame,
                                            String[] contents_of_the_box, String createName,
                                            String updatePageId,
                                            String updateVersionNumber, HttpSession session) {
        Date date = new Date();
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        String username = meaningUser.getUsername();
        Template template = new Template();
        Integer count = 0;
        System.out.println(updateVersionNumber);
        Double number =  Double.parseDouble(updateVersionNumber)+1;
        String versionNumber = String.valueOf( number );
        for (int i = 0; i < title.length; i++) {
            template.setTitle(title[i]);
            template.setFieldName(field_name[i]);
            template.setTypeOfFrame(type_of_frame[i]);
            template.setContentsOfTheBox(contents_of_the_box[i]);
            template.setPageId(updatePageId);
            template.setUpdatedDate(date);
            template.setCreationDate(date);
            template.setDepartmentId(meaningUser.getDepartmentId());
            template.setUpdate(username);
            template.setVersionNumber(versionNumber);
            template.setState(1);
            count = templateMapper.insertSelective(template);
        }
        return count;
    }


    /*-------------------------------------(没有接口方法)--------------------------------------------*/

    /**
     * 根据页面ID查询，模板中部分数据
     * @param pageId
     * @return
     */
    public Integer selectByPageId( String pageId, String updatedDate){
        return templateMapper.selectByPageId(pageId, updatedDate);
    }

}
